package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.constant.SuperConstant;
import com.zzyl.dto.RoleDto;
import com.zzyl.entity.Role;
import com.zzyl.entity.RoleResource;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.RoleMapper;
import com.zzyl.mapper.RoleResourceMapper;
import com.zzyl.mapper.UserRoleMapper;
import com.zzyl.service.RoleService;
import com.zzyl.vo.RoleVo;
import org.assertj.core.util.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private RoleResourceMapper roleResourceMapper;


    @Autowired
    private UserRoleMapper userRoleMapper;
    /**
     * 新增
     * @param roleDto
     * @return
     */
    @Override
    public Boolean add(RoleDto roleDto) {
        Role role = BeanUtil.toBean(roleDto, Role.class);
        role.setUpdateTime(LocalDateTime.now());
        role.setCreateTime(LocalDateTime.now());
        role.setCreateBy(1671403256519078006L);
        role.setRemark(roleDto.getRemark());
        return roleMapper.insert(role);
    }

    @Override
    public List<String> findCheckedResources(long l) {
      return   roleMapper.findCheckedResources(l);
    }

    @Override
    public Boolean update(RoleDto roleDto) {
        Role role = BeanUtil.toBean(roleDto, Role.class);
        role.setUpdateTime(LocalDateTime.now());
        role.setUpdateBy(1671403256519078006L);
        role.setCreateTime(LocalDateTime.now());
        role.setCreateBy(1671403256519078006L);
        role.setRemark(roleDto.getRemark());
        //状态的修改
        if(null !=roleDto && SuperConstant.DATA_STATE_1.equals(roleDto.getDataState())){
            Integer count =userRoleMapper.countByRoleId(roleDto.getId());
            if(null != count && count>0){
                throw new BaseException(BasicEnum.ROLE_DISTRIBUTED);
            }
        }
        Boolean flag = roleMapper.update(role);
        //重新构建角色和资源之间的关系，先删除，然后判断前端是否传递了资源数据
        roleResourceMapper.deleteByRoleId(roleDto.getId()+"");
        if(ObjectUtil.isEmpty(roleDto.getCheckedResourceNos())){
        return flag;
        }
        /**
         * 添加资源
         */
        List<RoleResource> roleResourceList = Lists.newArrayList();
        Arrays.asList(roleDto.getCheckedResourceNos()).stream().map(no->{
            RoleResource roleResource = new RoleResource();
            roleResource.setRoleId(roleDto.getId());
            roleResource.setResourceNo(no);
            roleResource.setCreateTime(LocalDateTime.now());
            roleResource.setUpdateTime(LocalDateTime.now());
            roleResource.setDataState(SuperConstant.DATA_STATE_0);
            roleResourceList.add(roleResource);
            return roleResource;
        }).collect(Collectors.toList());
        roleResourceMapper.batchInsert(roleResourceList);
        return flag;
    }

    @Override
    public PageResponse<RoleVo> list(RoleDto roleDto, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<RoleVo> vo =roleMapper.list(roleDto.getRoleName());
        return PageResponse.of(vo,RoleVo.class);

    }


    @Transactional
    @Override
    public void delete(String roleIds) {
        //1.通过id查询角色信息，然后判断状态，如果禁用则删除，不是则报错
        Role role = roleMapper.selectById(roleIds);
        if(null != role && SuperConstant.DATA_STATE_0.equals(role.getDataState())){
            throw new BaseException(BasicEnum.PARENT_ROLE_STATUS);
        }
        //2.删除角色和关联数据
        roleResourceMapper.deleteByRoleId(roleIds);
        //3.删除角色数据
        roleMapper.delete(roleIds);
    }


}
